package lyl.my;

import java.util.Scanner;
import java.util.Stack;


// 定义一个类名为 ClearString
public class ClearString {
    // 定义一个静态方法名为 clear，参数为字符串类型的数组
    public static String clear(String str) {
        if(str == null || "".equals(str) || str.length() <= 1){
            return str;
        }
        Stack<Character> stack = new Stack<>();
        stack.push(str.charAt(0));
        for (int i = 1; i < str.length(); i++) {

            if(!stack.isEmpty() && stack.peek().equals(str.charAt(i))){
                stack.pop();

            }else {
                stack.push(str.charAt(i));
            }
        }

        StringBuilder builder = new StringBuilder();
        while(!stack.isEmpty()){
            builder.append(stack.pop());
        }
        StringBuilder result = new StringBuilder();
        for(int i = builder.length() - 1; i >= 0; i--){
            result.append(builder.charAt(i));
        }
        return result.toString();
    }

    public static void main(String[] args) {
        System.out.println(clear("cbbcbd"));
    }
}